dojo.require("selfpass.module.Account");
dojo.require("selfpass.module.Catalog");

//Event Handler : [menu : onclick]
//menu events
function createAccountButtonEvt(evt){
	dojo.xhrPost({
		url: "/service/showCatalog",
		handleAs: "json",
		handle: function(data,args){
			var catalog = new selfpass.module.Catalog(data.key, data.name,data.des);
			var account = new selfpass.module.Account("","","","","","");
			account.setCatalog(catalog);
			showAccountDetails(account,true);
		},
		content:{"key":currentRootCatalogKey}
	});
}
function modifyAccountButtonEvt(evt){
	dojo.xhrPost({
				url: "/service/showAccount",
				handleAs: "json",
				handle: function(data,args){
					var account = new selfpass.module.Account(
						data.key,data.name,data.accountId,data.password,data.url,data.des);
					account.setCatalog(new selfpass.module.Catalog(data.catalogKey, data.catalogName));
					//decrypt pwd
					account.pwd = decrypt(account.pwd);
					showAccountDetails(account,false);
				},
				content:{"key":currentAccountKey}
		});		
}
function deleteAccountButtonEvt(evt){
	dojo.xhrPost({
			url: "/service/deleteAccount",
			handleAs: "json",
			handle: function(data,args){
				if(data.message && data.message=="success"){
					showAccountList(currentRootCatalogKey);
					//reset details space
					if(dojo.byId("account_key") && 
						dojo.byId("account_key").value == data.key){
							clearDiv("detailsSpace");
					}
				}else{
					alert("error")
				}			
			},
			content:{"key":currentAccountKey}
	});
}
function createCatalogButtonEvt(evt){	
	dojo.xhrPost({
		url: "/service/showCatalog",
		handleAs: "json",
		handle: function(data,args){
			var parentCatalog = new selfpass.module.Catalog(data.key, data.name,data.des);
			showCatalogDetails(parentCatalog,true);
		},
		content:{"key":currentRootCatalogKey}
	});
}
function modifyCatalogButtonEvt(evt){
	dojo.xhrPost({
			url: "/service/showCatalog",
			handleAs: "json",
			handle: function(data,args){
				var catalog = new selfpass.module.Catalog(data.key, data.name,data.des);
				catalog.setParent(
					new selfpass.module.Catalog(data.parentKey, data.parentName));
				showCatalogDetails(catalog,false);
			},
			content:{"key":currentRootCatalogKey}
	});		
}
function deleteCatalogButtonEvt(evt){
	dojo.xhrPost({
			url: "/service/deleteCatalog",
			handleAs: "json",
			handle: function(data,args){
				if(data.message && data.message=="success"){
					showTree();
					//reset details space
					if((dojo.byId("catalog_key") && 
						dojo.byId("catalog_key").value == data.key) ||
							(dojo.byId("account_catalog_key") && 
								dojo.byId("account_catalog_key").value == data.key)){
							clearDiv("detailsSpace");
					}
					//reset accountList space
					if(currentRootCatalogKey && 
						currentRootCatalogKey == data.key){	
							handleWidgetDestroy(dojo.byId("accountList"));
					}
				}else{
					alert("error")
				}			
			},
			content:{"key":currentRootCatalogKey}
	});
}

function clickAccountRow(evt){
		currentAccountKey = dijit.byId("accountListGrid")
			.model.getDatum(
				evt.rowIndex,
				dijit.byId("accountListGrid").model.fields.indexOf("key"));
		//set modify and delete abled
		dijit.byId("_modifyAccount").setDisabled(false);
		dijit.byId("_deleteAccount").setDisabled(false);
}

//Event Handler : [tree : click]
function clickCatalogNode(item,node){
	currentRootCatalogKey = item.key;
	currentRootCatalogNode = node;

	//set delete abled
	if(item.type == "root"){		 
			dijit.byId("_deleteCatalog").setDisabled(true);
	}else{
			dijit.byId("_deleteCatalog").setDisabled(false);
	}

	showAccountList(currentRootCatalogKey);
}

//Event Handler : [account list : dbclick]
//show accountDetails
function dblClickAccountRow(evt){
		var row = evt.rowIndex;
		var key = dijit.byId("accountListGrid")
			.model.getDatum(
				evt.rowIndex,
				dijit.byId("accountListGrid").model.fields.indexOf("key"));
		currentAccountKey = key;
		dojo.xhrPost({
				url: "/service/showAccount",
				handleAs: "json",
				handle: function(data,args){
					var account = new selfpass.module.Account(
						data.key,data.name,data.accountId,data.password,data.url,data.des);
					account.setCatalog(new selfpass.module.Catalog(data.catalogKey, data.catalogName));
					//decrypt pwd
					account.pwd = decrypt(account.pwd);
					showAccountDetails(account,false);
				},
				content:{"key":key}
		});		
}

